// SHCameraPreviewVC.m

/**************************************************************************
 *
 *       Copyright (c) 2014-2018年 by iCatch Technology, Inc.
 *
 *  This software is copyrighted by and is the property of iCatch
 *  Technology, Inc.. All rights are reserved by iCatch Technology, Inc..
 *  This software may only be used in accordance with the corresponding
 *  license agreement. Any unauthorized use, duplication, distribution,
 *  or disclosure of this software is expressly forbidden.
 *
 *  This Copyright notice MUST not be removed or modified without prior
 *  written consent of iCatch Technology, Inc..
 *
 *  iCatch Technology, Inc. reserves the right to modify this software
 *  without notice.
 *
 *  iCatch Technology, Inc.
 *  19-1, Innovation First Road, Science-Based Industrial Park,
 *  Hsin-Chu, Taiwan, R.O.C.
 *
 **************************************************************************/
 
 // Created by zj on 2018/3/22 上午11:13.
    

#import "SHCameraPreviewVC.h"
#import "SHCameraPreviewVCPrivateHeader.h"
#import <SDAutoLayout.h>
#import "AppDelegate.h"
#import <Masonry.h>

#define ENABLE_AUDIO_BITRATE 0

@interface SHCameraPreviewVC ()

@property (nonatomic, assign) BOOL isFullscreenMode;
@property (nonatomic, assign) BOOL isRotation;
@property (nonatomic, assign) CGRect originFrame;

@end

@implementation SHCameraPreviewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupGUI];
}

- (void)setupGUI {
    [self setupTopToolView];
    [self setupBottomToolView];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    //关闭旋转
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.isRotation = NO;
    
    [self interfaceOrientation:UIInterfaceOrientationPortrait];
}

- (IBAction)btnClick:(id)sender {
    NSLog(@"旋转");
    
    //支持旋转
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.isRotation = YES;
    
    [self updateSubviewFrameForFullScreen];
    
    [self interfaceOrientation:UIInterfaceOrientationLandscapeLeft];
}

- (IBAction)shrinkScreenClick:(id)sender {
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.isRotation = NO;
    
    [self updateSubviewFrameForShrinkScreen];
    
    [self interfaceOrientation:UIInterfaceOrientationPortrait];
}

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
    //强制转换
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = orientation;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

- (void)updateSubviewFrameForFullScreen {
    self.previewImageView.sd_layout.leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .rightSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0);
    
    self.pvFailedLabel.sd_layout.centerXEqualToView(self.previewImageView)
    .centerYEqualToView(self.previewImageView);
    
    self.captureButton.sd_resetLayout.leftSpaceToView(self.speakerButton, 20)
    .rightSpaceToView(self.view, 24)
    .bottomSpaceToView(self.view, 22)
    .widthIs(60)
    .heightEqualToWidth();

    self.speakerButton.sd_resetLayout.leftSpaceToView(self.audioButton, 20)
    .rightSpaceToView(self.captureButton, 20)
    .centerYEqualToView(self.captureButton)
    .widthIs(80)
    .heightEqualToWidth();
    
    self.audioButton.sd_resetLayout.rightSpaceToView(self.speakerButton, 20)
    .centerYEqualToView(self.speakerButton)
    .widthRatioToView(self.captureButton, 1.0)
    .heightEqualToWidth();
    
    self.headerView.hidden = YES;
    self.footerView.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    self.topToolView.hidden = NO;
    self.bottomToolView.hidden = NO;
}

- (void)updateSubviewFrameForShrinkScreen {    
    self.previewImageView.sd_layout.topSpaceToView(self.headerView, 0)
    .bottomSpaceToView(self.footerView, 0);
    
    self.speakerButton.sd_resetLayout.topSpaceToView(self.footerView, 44)
    .leftSpaceToView(self.audioButton, 40)
    .rightSpaceToView(self.captureButton, 40)
    .centerXEqualToView(self.view);
    
    self.captureButton.sd_resetLayout.centerYEqualToView(self.speakerButton)
    .leftSpaceToView(self.speakerButton, 40);
    
    self.audioButton.sd_resetLayout.centerYEqualToView(self.speakerButton)
    .rightSpaceToView(self.speakerButton, 40);
    
    self.headerView.hidden = NO;
    self.footerView.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.topToolView.hidden = YES;
    self.bottomToolView.hidden = YES;
}

#pragma mark - Setup Top Tool
- (void)setupTopToolView {
    UIView *topToolView = [[UIView alloc] init];
    
    topToolView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.75];
    topToolView.hidden = YES;
    
    [self.view addSubview:topToolView];

    topToolView.sd_layout.leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .rightSpaceToView(self.view, 0)
    .heightIs(44);
    
    self.topToolView = topToolView;
    
    [self setupCloseButton];
    [self setupTitleLabel];
    [self setupBatteryInfoImageView];
    [self setupBatteryInfoLabel];
}

- (void)setupCloseButton {
    UIButton *closeBtn = [[UIButton alloc] init];
    
    closeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [closeBtn setTitle:@"Close" forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(shrinkScreenClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.topToolView addSubview:closeBtn];

    closeBtn.sd_layout.leftSpaceToView(self.topToolView, 12)
    .topSpaceToView(self.topToolView, 0)
    .bottomSpaceToView(self.topToolView, 0)
    .widthEqualToHeight();
    
    self.topCloseBtn = closeBtn;
}

- (void)setupTitleLabel {
    UILabel *titleLable = [[UILabel alloc] init];
    
    titleLable.text = @"Title";
    titleLable.textColor = [UIColor whiteColor];
    titleLable.font = [UIFont systemFontOfSize:16];
    
    [self.topToolView addSubview:titleLable];
    
    titleLable.sd_layout.topSpaceToView(self.topToolView, 0)
    .bottomSpaceToView(self.topToolView, 0)
    .centerXEqualToView(self.topToolView)
    .centerYEqualToView(self.topToolView)
    .autoWidthRatio(1.0);
    
//    [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.topToolView);
//        make.bottom.mas_equalTo(self.topToolView);
//        make.center.mas_equalTo(self.topToolView);
//    }];
    
    self.topTitleLabel = titleLable;
}

- (void)setupBatteryInfoImageView {
    UIImageView *batteryInfoImgView = [[UIImageView alloc] init];
    
    [self.topToolView addSubview:batteryInfoImgView];
    
    batteryInfoImgView.sd_layout.topSpaceToView(self.topToolView, 0)
    .rightSpaceToView(self.topToolView, 24)
    .bottomSpaceToView(self.topToolView, 0)
    .leftSpaceToView(self.batteryInfoLabel, -8)
    .widthEqualToHeight()
    .centerYEqualToView(self.topToolView);
    
    self.batteryInfoImgView = batteryInfoImgView;
}

- (void)setupBatteryInfoLabel {
    UILabel *batteryInfoLabel = [[UILabel alloc] init];
    
    batteryInfoLabel.font = [UIFont systemFontOfSize:17.0];
    CGFloat colorValue = 242 / 255.0;
    batteryInfoLabel.textColor = [UIColor colorWithRed:colorValue green:colorValue  blue:colorValue alpha:1.0];
    batteryInfoLabel.text = @"70%";
    
    [self.topToolView addSubview:batteryInfoLabel];
    
    batteryInfoLabel.sd_layout.topSpaceToView(self.topToolView, 0)
    .bottomSpaceToView(self.topToolView, 0)
    .centerYEqualToView(self.batteryInfoImgView)
    .rightSpaceToView(self.batteryInfoImgView, -8)
    .autoWidthRatio(1.0);
    
    self.batteryInfoLabel = batteryInfoLabel;
}

- (void)setupBottomToolView {
    UIView *bottomToolView = [[UIView alloc] init];
    
    bottomToolView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    bottomToolView.hidden = YES;
    
    [self.view addSubview:bottomToolView];
    
    bottomToolView.sd_layout.leftSpaceToView(self.view, 16)
    .bottomSpaceToView(self.view, 12)
    .heightIs(24)
    .widthIs(80);
    
    bottomToolView.sd_cornerRadius = @(5);
    
    self.bottomToolView = bottomToolView;
    
    [self setupBitRateInfoLable];
}

- (void)setupBitRateInfoLable {
    UILabel *bitRateInfoLabel = [[UILabel alloc] init];
    
    bitRateInfoLabel.font = [UIFont systemFontOfSize:17.0];
    CGFloat colorValue = 242 / 255.0;
    bitRateInfoLabel.textColor = [UIColor colorWithRed:colorValue green:colorValue  blue:colorValue alpha:1.0];
    bitRateInfoLabel.text = @"1122kb/s";
    bitRateInfoLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.bottomToolView addSubview:bitRateInfoLabel];
    
    bitRateInfoLabel.sd_layout.leftSpaceToView(self.bottomToolView, 4)
    .rightSpaceToView(self.bottomToolView, 4)
    .topSpaceToView(self.bottomToolView, 0)
    .bottomSpaceToView(self.bottomToolView, 0);
    
    self.bitRateInfoLabel = bitRateInfoLabel;
}

@end
